package com.android.lovingpet.signup;

import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.widget.TextView;


import com.android.lovingpet.Application;
import com.android.lovingpet.R;
import com.android.lovingpet.SignModel;
import com.android.lovingpet.base.BasePresenter;
import com.android.lovingpet.common.Back;
import com.android.lovingpet.common.Model;
import com.android.lovingpet.util.ImageUtil;
import com.android.lovingpet.util.Utility;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by ikata on 2018/2/3.
 */

public class LoginInPresenter extends BasePresenter<ILoginContract.View> implements ILoginContract.Presenter{

    private String sessionId;
    private String phone;
    private String password;
    private int userType;
    private SignModel login_in_model;

    public LoginInPresenter() {
        sessionId = "";
        login_in_model = new SignModel();
    }

    /**
     * @param userType 	0：组织 1：个人
     */
    @Override
    public void next(final String userName, final String password, int userType) {
        if (TextUtils.isEmpty(userName) || userName.length() != 11) {
            getView().showToast("手机号不可用!");
            return;
        }
        if (TextUtils.isEmpty(password) || password.length() < 6) {
            getView().showToast("密码最少6位!");
            return;//未判断混合密码
        }
        this.phone = userName;
        this.password = password;
        this.userType = userType;
        sendCode();
        getView().next();
    }

    @Override
    public String getPhone() {
        return phone;
    }

    @Override
    public void auth(String code) {
        if (TextUtils.isEmpty(code) || code.length() != 4) {
            getView().showToast("验证码必须为四位数！");
            return;
        }
        getView().showProgress();
        authCode(code);
    }

    @Override
    public void commitInfo(File image, String name, int gender) {
        if (TextUtils.isEmpty(name)) {
            getView().showToast("请填写昵称");
            return;
        }
        getView().showProgress();
        login_in_model.commitPersonInfo(Utility.getRealId(), Utility.getUserType(), image, name, gender, new Back() {
            @Override
            public void onFailure(String msg) {
                getView().hideProgress();
                getView().showToast(msg);
            }

            @Override
            public void onSuccess(@Nullable Bundle bundle) {
                getView().next();
            }
        });
    }

    @Override
    public void commitInfo(File image, String name, String address, String ETime, String nature, String HName, String HPhone, String email, String OPhone, String code) {
        if (image == null) {
            getView().showToast("请上传组织标志");
            return;
        }
        if (TextUtils.isEmpty(name) || TextUtils.isEmpty(email) || TextUtils.isEmpty(address) || TextUtils.isEmpty(ETime) || TextUtils.isEmpty(nature) || TextUtils.isEmpty(HName) || TextUtils.isEmpty(HPhone) || TextUtils.isEmpty(OPhone)) {
            getView().showToast("请填写所有必填项");
            return;
        }
        if (HPhone.length() != 11 || OPhone.length() != 11 || !email.contains("@")) {
            getView().showToast("请填写正确的电话号码和邮箱地址");
            return;
        }
        getView().showProgress();
        login_in_model.commitTeamInfo(Utility.getRealId(), image, name, address, ETime, nature, HName, HPhone, email, OPhone, code, new Back() {
            @Override
            public void onFailure(String msg) {
                getView().hideProgress();
                getView().showToast(msg);
            }

            @Override
            public void onSuccess(@Nullable Bundle bundle) {
                getView().next();
            }
        });
    }

    @Override
    public void sendCode() {
        login_in_model.sendCode(phone, new Back() {
            @Override
            public void onFailure(String msg) {
                getView().showToast(msg);
            }

            @Override
            public void onSuccess(@Nullable Bundle bundle) {
                sessionId = bundle.getString("JSessionId");
            }
        });
    }

    private void authCode(String code) {
        String formTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(System.currentTimeMillis()));;
        login_in_model.authCode(sessionId, phone, code, formTime, new Back() {
            @Override
            public void onFailure(String msg) {
                getView().hideProgress();
                getView().showToast(msg);
            }

            @Override
            public void onSuccess(@Nullable Bundle bundle) {
                signUp();
            }
        });
    }

    private void signUp() {
        login_in_model.sign(0, phone, password, userType, new Back() {
            @Override
            public void onFailure(String msg) {
                getView().hideProgress();
                getView().showToast(msg);
            }

            @Override
            public void onSuccess(@Nullable Bundle bundle) {
                getView().hideProgress();
                getView().next();
            }
        });
    }
}
